/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.saving;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import src.domain.gl.Tax;
import src.domain.saving.Saving;
import src.domain.saving.Savpurpose;
import src.domain.security.Users;
import src.domain.system.Numbering;
import src.services.CRUEDService;
import src.services.gl.TaxModel;
import src.services.saving.SavingModel;
import src.services.saving.SavpurposeModel;
import src.util.Constanta;
import src.util.Messager;
import src.view.security.LoginBean;
import src.view.system.ApprovalBean;

/**
 *
 * @author jasuke
 */
@ManagedBean
@SessionScoped
public class SavingApprovalBean {

    private List<Savpurpose> avaiblePurpose;
    private Saving saving;
    private List<Saving> savings;
    private SavpurposeModel purHelper;
    private SavingModel helper;
    private boolean editMode = false;
    private Users user;
    private ApprovalBean approvalBean;
    
    public SavingApprovalBean() {
        ambilUser();
        this.purHelper    = new SavpurposeModel();
        this.helper       = new SavingModel();  
        FacesContext context = FacesContext.getCurrentInstance();
        approvalBean = (ApprovalBean) context.getApplication().evaluateExpressionGet(context, "#{approvalBean}", ApprovalBean.class);
        approvalBean.setUpdateTab(false);
    }

    public Saving getSaving() {
        return saving;
    }

    public void setSaving(Saving saving) {
        this.saving = saving;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public List<Saving> getSavings() {
        return savings;
    }

    public List<Savpurpose> getAvaiblePurpose() {
        return avaiblePurpose;
    }

    public Double getInterestTaxName(Integer id) {  
        TaxModel tm = new TaxModel();
        Double txRate = null;
        Tax tax = tm.getById(id);
        
        if (tax != null) txRate = tax.getTaxRate().doubleValue();
        return txRate;
    }
    
    private void initForm() {
        this.avaiblePurpose = purHelper.findAll(null);
        this.editMode = true;
    }
    
    public void openData() {
        approvalBean.setUpdateTab(false);
        initForm();
    }
    
    public void handleClose() {
        this.saving = null;
    }
    
    public final void loadSavings () {
        Map<String, String> savfilter = new HashMap<>();
        savfilter.put("approvalUid", null);
        savfilter.put("closeDate", null);
        savfilter.put("closedBy", null);
        savfilter.put("order", "accountNo");
                
        this.savings = helper.findAll(savfilter);
    }
    
    private void newNumber() {
        if (saving.getAccountNo().equals(Constanta.AUTO_NUMBERING)) {
            Numbering currentNo = new CRUEDService().getNextTrxNumber(1, saving.getBranchId(), saving.getSavprod().getSavproductId(), 1);
            saving.setAccountNo(currentNo.getTrxNum());
        }
    }
    
    public void approve(boolean approved) {
        RequestContext context = RequestContext.getCurrentInstance();
        boolean success;  
        
        if (approved==true) {
            newNumber();
            saving.setApprovalUid(user.getUserId());
        }
        else {
            saving.setClosedBy(Constanta.SAV_CLOSED_BY_SYSTEM);
            saving.setCloseDate(new Date());
        }
        
        success = helper.saveData(saving);
        
        if (success) {
            this.savings.remove(this.saving);
            approvalBean.updateTabOtor();
       //     this.saving = null;
        } 
        
        Messager.showSaveUpdateStatus(success);     
        context.addCallbackParam("success", success);  
    }
    
    private void ambilUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean logBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        this.user = logBean.getSelectedUser();
    }
}
